//重写数组的方法 7个：push shift unshift pop reverse sort splice会导致数组发生变化
let oldArrayMethods = Array.prototype;

export const arrayMethods = Object.create(oldArrayMethods);

const methods = [
  'push',
  'shift',
  'unshift',
  'reverse',
  'sort',
  'pop',
  'splice'
];

methods.forEach(method=>{
  arrayMethods[method] = function(...args){
    console.log('调用push方法'); //AOP切片编程
    const result = oldArrayMethods[method].apply(this,args);//调用原生method
    let inserted;
    let ob = this.__ob__;
    switch(method){
      case 'push':
      case 'unshift':
        inserted = args;
        break;
      case 'splice':
        inserted = args.slice(2);
        break;
      default:
        break;
    }
    if(inserted){
        ob.observerArray(inserted) //将新增属性继续监测
    }
    ob.dep.notify();
    return result;
  }
})
